package widget.book.viewer
{
	import flash.display.DisplayObject;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.geom.Point;
	
	public class BookViewer extends Sprite{
		public static const TYPE_PAGE_FRONT:String = "frontCover";     //封面
		public static const TYPE_PAGE_BACK:String = "backCover";       //封底
		public static const TYPE_PAGE_CONTENT:String = "contentPage";  //内容页
		
		private var debugMc:Shape = null;
		//-----------------------------
		private var _size:Point = null;
		
		private var pageList:Vector.<BasePageViewer> = null;      //
		private var pageSourceList:Vector.<DisplayObject> = null; //
		                                         
		private var dragPage:BasePageViewer = null;               //
		private var currentPage:DisplayObject = null;             //  
		private var fCover:BasePageViewer = null;
		private var bCover:BasePageViewer = null;
		private var currentPageNum:uint = 0;
		
		public function BookViewer(wid:Number,hei:Number)
		{
			super();
			_size = new Point(wid,hei);
			pageList = new Vector.<BasePageViewer>();
			pageSourceList = new Vector.<DisplayObject>();
		}
		
		public function init():void {
			if (fCover == null) return;
			currentPage = fCover;
			addChild(currentPage);
			fCover.x = _size.x;
		}
		
		/*----------------------------------------
		* This function is for debug
		* --------------------------------------*/
		private function test():void {
			debugMc = new Shape();
			
			debugMc.graphics.lineStyle(2);
			debugMc.graphics.beginFill(0xff0000, 0.3);
			debugMc.graphics.drawRect(0, 0, _size.x, _size.y);
			debugMc.graphics.endFill();
			
			addChild(debugMc);
		}
		
		/**
		 * 添加页，包括封面和封底
		 * */
		public function addPage(page:BasePageViewer,type:String):void {
			if (type == TYPE_PAGE_FRONT) {
				fCover = page;
			}else if(type == TYPE_PAGE_BACK) {
				bCover = page;
			}else if(type == TYPE_PAGE_CONTENT){
				pageList.push(page);
				pageSourceList.push(page.sourceObject);
			}
		}
		
		public function setDragPage(num:int, area:Number):void {
			if (num == 0) {
				dragPage = fCover;
			}else if (num == pageList.length ) {
				dragPage = bCover;
			}else {
				dragPage = pageList[num-1];
			}
			
			dragPage.position = area;
			addChild(dragPage);
		}
		
		public function update(isNext:Boolean):void {
			if (isNext) {
				if (currentPage != null) removeChild(currentPage);
				currentPage = dragPage.sourceObject;
				if (dragPage is CoverViewer) currentPage.x = _size.x/2;
				addChild(currentPage);
			}
			
			if (dragPage != null) removeChild(dragPage);
		}
		
		public function get pageDrag():BasePageViewer {
			return dragPage;
		}
	}
}